const app = getApp()
let qCloud = require("../../index.js");

Page({
    /** 页面的初始数据 */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        produceTaskId: "", // 生产任务ID
        upcountProcessList: [], // 员工上数工序列表
        produceTaskProcessList: [], // 生产任务的工序列表，展示上数情况和选择工序时使用
        checkedProduceTaskIds: [],

        promptMark: false,
        showMark: false,
        arrowRight: false,
        showModalStatus: false,
    },

    showDetailTab: function (e) {
        this.setData({
            arrowRight: !this.data.arrowRight
        })
    },

    // 提交数量
    submitTotalInput(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;

        const upcountProcess = self.data.upcountProcessList[index];
        // 工序总量
        let processTotal = Number(upcountProcess["processTotal"]);
        // 完成数量
        let completedProcess = Number(upcountProcess["completedProcess"]);
        // 审批数量
        let approvalProcess = Number(upcountProcess["approvalProcess"]);

        // 新提交数量
        let submitTotal = e.detail.value == "" ? "" : Number(e.detail.value);
        self.data.upcountProcessList[index]["submitTotal"] = submitTotal;

        // 新剩余数量 = 工序总量 - 完成数量 - 审批数量 - 新提交数量
        let unfinishedQuantity = processTotal - completedProcess - approvalProcess - submitTotal;
        self.data.upcountProcessList[index]["unfinishedQuantity"] = unfinishedQuantity;

        self.setData({
            upcountProcessList: self.data.upcountProcessList
        });
    },

    hideProcessModal: function (e) {
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export(),
                produceTaskProcessList: [],
                showModalStatus: false
            })
        }.bind(this), 200);
    },

    // 添加工序
    addTap: function () {
        this.setData({
            checkedProduceTaskIds: [],
            showModalStatus: true
        })
    },

    // 工序选择(多选)
    checkboxChange: function (e) {
        this.setData({
            checkedProduceTaskIds: e.detail.value
        })
    },

    // 工序选择(确认)
    confirmCheckboxChange: function () {
        if (this.data.checkedProduceTaskIds.length > 0) {
            let produceTaskProcessList = this.data.produceTaskProcessList;
            let upcountProcessList = [];
            for (let i = 0; i < produceTaskProcessList.length; i++) {
                for (let j = 0; j < this.data.checkedProduceTaskIds.length; j++) {
                    if (produceTaskProcessList[i].produceTaskProcessId == this.data.checkedProduceTaskIds[j]) {
                        produceTaskProcessList[i].checkMark = true;
                        upcountProcessList.push(produceTaskProcessList[i])
                        break;
                    } else {
                        produceTaskProcessList[i].checkMark = false;
                    }
                }
            }
            this.setData({
                upcountProcessList: upcountProcessList,
                produceTaskProcessList: produceTaskProcessList,
                showModalStatus: false
            });
        }
    },

    // 删除工序
    delTap(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;

        // 把选中的工序改为不显示
        let upcountProcessInfo = self.data.upcountProcessList[index];
        for (let i = 0; i < self.data.produceTaskProcessList.length; i++) {
            let produceTaskProcessInfo = self.data.produceTaskProcessList[i];
            if (upcountProcessInfo.produceTaskProcessId == produceTaskProcessInfo.produceTaskProcessId) {
                // 剩余数量 = 工序总量 - 完成数量 - 审批数量
                let unfinishedQuantity = produceTaskProcessInfo.processTotal - produceTaskProcessInfo.completedProcess - produceTaskProcessInfo.approvalProcess;
                self.data.produceTaskProcessList[i]["unfinishedQuantity"] = unfinishedQuantity;
                self.data.produceTaskProcessList[i]["checkMark"] = false;
                self.data.produceTaskProcessList[i]["submitTotal"] = "";
                break;
            }
        }

        // 删除工序
        self.data.upcountProcessList.splice(index, 1);
        self.setData({
            upcountProcessList: self.data.upcountProcessList,
            produceTaskProcessList: self.data.produceTaskProcessList
        });
    },

    /** 生命周期函数--监听页面加载 */
    onLoad: function (options) {
        let self = this;
        self.setData({
            produceTaskId: options.produceTaskId
        });
        // 查询生产任务的工序列表
        self.queryProduceTaskProcessList();
    },

    /** 查询生产任务的工序列表 */
    queryProduceTaskProcessList: function () {
        let self = this;

        let params = {
            produceTaskId: self.data.produceTaskId, // 生产任务ID
        }
        app.fetchApis(self, qCloud.api.producetask_queryProduceTaskProcessList, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                let data = result.data;

                for (let i = 0; i < data.produceTaskProcessList.length; i++) {
                    let produceTaskProcessInfo = data.produceTaskProcessList[i];
                    // 剩余数量 = 工序总量 - 完成数量 - 审批数量
                    let unfinishedQuantity = produceTaskProcessInfo.processTotal - produceTaskProcessInfo.completedProcess - produceTaskProcessInfo.approvalProcess;
                    data.produceTaskProcessList[i]["unfinishedQuantity"] = unfinishedQuantity;
                    data.produceTaskProcessList[i]["checkMark"] = false;
                    data.produceTaskProcessList[i]["submitTotal"] = "";
                }

                self.setData({
                    produceTaskProcessList: data.produceTaskProcessList
                });

                self.queryStaffUpcountProcessList();
            }
        });
    },

    /** 查询生产任务中员工的上数工序集合 */
    queryStaffUpcountProcessList: function () {
        let self = this;

        let params = {
            produceTaskId: self.data.produceTaskId, // 生产任务ID
        }
        app.fetchApis(self, qCloud.api.workreport_queryStaffUpcountProcessList, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                let data = result.data;

                if (data.upcountProcessList && data.upcountProcessList.length > 0) {
                    let upcountProcessList = [];
                    for (let i = 0; i < self.data.produceTaskProcessList.length; i++) {
                        for (let j = 0; j < data.upcountProcessList.length; j++) {
                            if (self.data.produceTaskProcessList[i].produceTaskProcessId == data.upcountProcessList[j].produceTaskProcessId) {
                                self.data.produceTaskProcessList[i].checkMark = true;
                                upcountProcessList.push(self.data.produceTaskProcessList[i])
                            }
                        }
                    }

                    self.setData({
                        produceTaskProcessList: self.data.produceTaskProcessList,
                        upcountProcessList: upcountProcessList
                    });
                }
            }
        });
    },

    /** 提交工序的上数数量 */
    submitStaffUpcount: function () {
        let self = this;
        self.setData({
            showMark: true
        });

        let mark = false; // 是否超数
        let markTime = 1000; // 设置等待时间
        let produceTaskProcessList = [];
        for (let i = 0; i < self.data.upcountProcessList.length; i++) {
            let upcountProcessInfo = self.data.upcountProcessList[i];
            if (Number(upcountProcessInfo.submitTotal) > 0) {
                if (!mark && Number(upcountProcessInfo.unfinishedQuantity) < 0) {
                    mark = true; // 超数，显示提示语
                    markTime = 3000; // 超数，页面停留时间
                }
                produceTaskProcessList.push(upcountProcessInfo);
            }
        }

        if (produceTaskProcessList.length > 0) {
            let params = {
                produceTaskId: self.data.produceTaskId,
                produceTaskProcessList: produceTaskProcessList,
            }
            app.fetchApis(self, qCloud.api.workreport_submitStaffUpcount, params, "POST", function (e) {
                if (e.data.code == 200) {
                    self.setData({
                        promptMark: mark
                    });
                    setTimeout(function () {
                        self.navigateToIndex();
                    }.bind(this), markTime);
                }
            });
        } else {
            self.navigateToIndex();
        }
    },

    // 跳转列表页
    navigateToIndex() {
        wx.navigateTo({
            url: "/pages/report/index?tab=2",
        });
    },

    /** 生命周期函数--监听页面初次渲染完成 */
    onReady: function () {},
    /** 生命周期函数--监听页面显示 */
    onShow: function () {},
    /** 生命周期函数--监听页面隐藏 */
    onHide: function () {},
    /** 生命周期函数--监听页面卸载 */
    onUnload: function () {},
    /** 页面相关事件处理函数--监听用户下拉动作 */
    onPullDownRefresh: function () {},
    /** 页面上拉触底事件的处理函数 */
    onReachBottom: function () {},
    /** 用户点击右上角分享 */
    onShareAppMessage: function () {}
});
